package mahan.data.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mongodb.DBObject;

public class Product implements KeyValuePair {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Product from(DBObject dbo) {
		Map map = dbo.toMap();
		int pid = (Integer) map.get("pid");
		String sku = (String) map.get("sku");
		String bn = (String) map.get("bn");
		int vid = (Integer) map.get("vid");
		Product product = new Product(sku, bn, vid);
		product.setProductID(pid);
		product.put(map);
		return product;
	}

	public static Product instance() {
		return new Product();
	}

	private int productID;
	private String sku;
	private String brandName;
	private int vendorID;
	private boolean mens;
	private boolean womens;
	private boolean bigKids;

	private boolean littleKids;
	private List<Float> menSizes = new ArrayList<Float>();
	private List<Float> womenSizes = new ArrayList<Float>();
	private List<Float> littleKidSizes = new ArrayList<Float>();
	private List<Float> bigKidSizes = new ArrayList<Float>();
	private double costPrice;
	private double sellingPrice;

	private double discountPrice;

	private Product() {
	}

	public Product(String sku, String brandName, int manufacturerID) {
		this.sku = sku;
		this.brandName = brandName;
		this.vendorID = manufacturerID;
	}

	public Map<String, ?> get() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pid", productID);
		map.put("sku", sku);
		map.put("bn", brandName);
		map.put("vid", vendorID);
		map.put("m", mens);
		map.put("w", womens);
		map.put("bk", bigKids);
		map.put("lk", littleKids);

		map.put("ms", menSizes);
		map.put("ws", womenSizes);
		map.put("lks", littleKidSizes);
		map.put("bks", bigKidSizes);

		map.put("cost", costPrice);
		map.put("sell", sellingPrice);
		map.put("disc", discountPrice);
		return map;
	}

	public List<Float> getBigKidSizes() {
		return bigKidSizes;
	}

	public String getBrandName() {
		return brandName;
	}

	public double getCostPrice() {
		return costPrice;
	}

	public double getDiscountPrice() {
		return discountPrice;
	}

	public List<Float> getLittleKidSizes() {
		return littleKidSizes;
	}

	public List<Float> getMenSizes() {
		return menSizes;
	}

	public int getProductID() {
		return productID;
	}

	public double getSellingPrice() {
		return sellingPrice;
	}

	public String getSku() {
		return sku;
	}

	public int getVendorID() {
		return vendorID;
	}

	public List<Float> getWomenSizes() {
		return womenSizes;
	}

	public boolean isBigKids() {
		return bigKids;
	}

	public boolean isKids() {
		return bigKids || littleKids;
	}

	public boolean isLittleKids() {
		return littleKids;
	}

	public boolean isMens() {
		return mens;
	}

	public boolean isWomens() {
		return womens;
	}

	@SuppressWarnings("unchecked")
	public void put(Map<String, ?> map) {
		productID = (Integer) map.get("pid");
		sku = (String) map.get("sku");
		brandName = (String) map.get("bn");
		vendorID = (Integer) map.get("vid");
		mens = (Boolean) map.get("m");
		womens = (Boolean) map.get("w");
		bigKids = (Boolean) map.get("bk");
		littleKids = (Boolean) map.get("lk");

		menSizes = (List<Float>) map.get("ms");
		womenSizes = (List<Float>) map.get("ws");
		littleKidSizes = (List<Float>) map.get("lks");
		bigKidSizes = (List<Float>) map.get("bks");

		costPrice = (Double) map.get("cost");
		sellingPrice = (Double) map.get("sell");
		discountPrice = (Double) map.get("disc");
	}

	public void setBigKids(boolean bigKids) {
		this.bigKids = bigKids;
	}

	public void setBigKidSizes(List<Float> bigKidSizes) {
		this.bigKidSizes.addAll(bigKidSizes);
	}

	public void setCostPrice(double costPrice) {
		this.costPrice = costPrice;
	}

	public void setDiscountPrice(double discountPrice) {
		this.discountPrice = discountPrice;
	}

	public void setLittleKids(boolean littleKids) {
		this.littleKids = littleKids;
	}

	public void setLittleKidSizes(List<Float> littleKidSizes) {
		this.littleKidSizes.addAll(littleKidSizes);
	}

	public void setMens(boolean mens) {
		this.mens = mens;
	}

	public void setMenSizes(List<Float> menSizes) {
		this.menSizes.addAll(menSizes);
	}

	public void setProductID(int productID) {
		this.productID = productID;
	}

	public void setSellingPrice(double sellingPrice) {
		this.sellingPrice = sellingPrice;
	}

	public void setWomens(boolean womens) {
		this.womens = womens;
	}

	public void setWomenSizes(List<Float> womenSizes) {
		this.womenSizes.addAll(womenSizes);
	}
}